package com.demo.widget;

import com.meis.widget.utils.AttrUtils;
import com.meis.widget.utils.DensityUtil;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;

public class DemoToolbar extends DirectionalLayout implements Component.ClickedListener {
    private Context mContext;
    private Text mText;
    private String mTitle;
    private boolean mShowBackIcon = true;

    public DemoToolbar(Context context) {
        this(context, null);
    }

    public DemoToolbar(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }

    public DemoToolbar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        this.mContext = context;
        init(attrSet);
    }

    private void init(AttrSet attrSet) {
        setOrientation(DirectionalLayout.HORIZONTAL);
        ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig(
                ComponentContainer.LayoutConfig.MATCH_PARENT,
                DensityUtil.getDimensionPixelSize(mContext, ResourceTable.String_toolbar_height));
        setLayoutConfig(layoutConfig);
        int padding = DensityUtil.dp2px(mContext, 16);
        setPadding(padding, 0, padding, 0);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(new RgbColor(244, 66, 53));
        setBackground(shapeElement);
        if (attrSet != null) {
            mTitle = AttrUtils.getStringFromAttr(attrSet, "toolbar_title", "");
            mShowBackIcon = AttrUtils.getBooleanFromAttr(attrSet, "toolbar_show_back_icon", true);
        }
        addChild();
    }

    private void addChild() {
        if (mShowBackIcon) {
            Image image = new Image(mContext, null);
            image.setScaleMode(Image.ScaleMode.STRETCH);
            image.setPixelMap(ResourceTable.Media_ic_arrow_back_white_24dp);
            image.setClickedListener(this);
            final int desiredSize = DensityUtil.dp2px(mContext, 22);
            DirectionalLayout.LayoutConfig imageLayoutConfig =
                    new DirectionalLayout.LayoutConfig(desiredSize, desiredSize);
            imageLayoutConfig.alignment = LayoutAlignment.VERTICAL_CENTER;
            addComponent(image, imageLayoutConfig);
        }
        mText = new Text(mContext, null);
        mText.setTextColor(Color.WHITE);
        mText.setTextSize(20, Text.TextSizeType.FP);
        mText.setText(mTitle);
        DirectionalLayout.LayoutConfig textLayoutConfig =
                new DirectionalLayout.LayoutConfig(LayoutConfig.MATCH_CONTENT, LayoutConfig.MATCH_CONTENT);
        textLayoutConfig.alignment = LayoutAlignment.VERTICAL_CENTER;
        textLayoutConfig.setMarginLeft(DensityUtil.dp2px(mContext, mShowBackIcon ? 30 : 0));
        addComponent(mText, textLayoutConfig);
    }

    @Override
    public void onClick(Component component) {
        if (mContext != null) {
            mContext.terminateAbility();
        }
    }

    public void setTitle(int resId) {
        if (mText != null) {
            mText.setText(resId);
        }
    }
}
